package com.netpace.iem.ahmed;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Component;
import com.netpace.iem.ahmed.repository.LabelRepository;
import com.netpace.iem.ahmed.model.Label;

@Component
public class Main {
	private static ClassPathXmlApplicationContext context;
	private static Logger log = Logger.getLogger(Main.class);		
	
	@Autowired
	private LabelRepository _labelRepository;
	
	public Main() {
		
	}
	

	public static void main(String[] args) {
		context = new ClassPathXmlApplicationContext("application-context.xml");
        log.info("Spring context initialized.");
        
        Main main = (Main) context.getBean(Main.class);
        main.init();
	}


	private void init() {
		long count = _labelRepository.count(); 
		log.info("Old Labels count: " + count);
		if(count == 0) {
			for(int i = 0; i < 5; i++) {
				Label label = new Label();
				label.setName("Ahmed");
				label.setDescription("Ahmed");
				_labelRepository.save(label);
			}	
			count = _labelRepository.count(); 
		}		
		log.info("New Labels count: " + count);
		for(Label label : _labelRepository.findByName("Ahmed"))
			log.info(label.getName());
	}
}
